Skip to contentSkip to conversation

Xen/mem_event: Do not rely on the toolstack being bug-free

PATCH In review Andrew Cooper 7 participants 26 messages Jul 17, 2014 – Jul 18, 2014 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

cover Xen/mem_event: Do not rely on the toolstack being bug-free

Xen performs insufficient validation of the contents of mem_event responses from the toolstack. As a result, a buggy toolstack could cause Xen to walk off the end of a domain's vcpu list, and get out of sync with vcpu pause reference counts.

These two fixes are compile tested only, as I have no way to plausibly test the mem-event functionality itself.

Andrew Cooper (2):
Xen/mem_event: Validate the response vcpu_id before acting on it. Xen/mem_event: Prevent underflow of vcpu pause counts

 xen/arch/x86/hvm/hvm.c          |    2 +-
 xen/arch/x86/mm/mem_event.c     |   14 ++++++++++++++
 xen/arch/x86/mm/mem_sharing.c   |   13 +++++++++++--
 xen/arch/x86/mm/p2m.c           |   26 ++++++++++++++++++++++----
 xen/include/asm-x86/mem_event.h |    3 +++
 xen/include/xen/sched.h         |    2 ++
 6 files changed, 53 insertions(+), 7 deletions(-)
#1 lore raw

PATCH 1/2 Xen/mem_event: Validate the response vcpu_id before acting on it.

CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Andres Lagar-Cavilla <[email protected]>
---
 xen/arch/x86/mm/mem_sharing.c |   11 ++++++++++-
 xen/arch/x86/mm/p2m.c         |   22 ++++++++++++++++++++--
 2 files changed, 30 insertions(+), 3 deletions(-)
xen/arch/x86/mm/mem_sharing.c +10 -1
index 7293f31..ec99266 100644
@@ -596,11 +596,20 @@ int mem_sharing_sharing_resume(struct domain *d)
     /* Get all requests off the ring */
     while ( mem_event_get_response(d, &d->mem_event->share, &rsp) )
     {
+        struct vcpu *v;
+
         if ( rsp.flags & MEM_EVENT_FLAG_DUMMY )
             continue;
+
+        /* Validate the vcpu_id in the response. */
+        if ( (rsp.vcpu_id >= d->max_vcpus) || !d->vcpu[rsp.vcpu_id] )
+            continue;
+
+        v = d->vcpu[rsp.vcpu_id];
+
         /* Unpause domain/vcpu */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(d->vcpu[rsp.vcpu_id]);
+            vcpu_unpause(v);
     }
 
     return 0;
xen/arch/x86/mm/p2m.c +20 -2
index 642ec28..f213a39 100644
@@ -1290,8 +1290,17 @@ void p2m_mem_paging_resume(struct domain *d)
     /* Pull all responses off the ring */
     while( mem_event_get_response(d, &d->mem_event->paging, &rsp) )
     {
+        struct vcpu *v;
+
         if ( rsp.flags & MEM_EVENT_FLAG_DUMMY )
             continue;
+
+        /* Validate the vcpu_id in the response. */
+        if ( (rsp.vcpu_id >= d->max_vcpus) || !d->vcpu[rsp.vcpu_id] )
+            continue;
+
+        v = d->vcpu[rsp.vcpu_id];
+
         /* Fix p2m entry if the page was not dropped */
         if ( !(rsp.flags & MEM_EVENT_FLAG_DROP_PAGE) )
         {
@@ -1310,7 +1319,7 @@ void p2m_mem_paging_resume(struct domain *d)
         }
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(d->vcpu[rsp.vcpu_id]);
+            vcpu_unpause(v);
     }
 }
 
@@ -1418,11 +1427,20 @@ void p2m_mem_access_resume(struct domain *d)
     /* Pull all responses off the ring */
     while( mem_event_get_response(d, &d->mem_event->access, &rsp) )
     {
+        struct vcpu *v;
+
         if ( rsp.flags & MEM_EVENT_FLAG_DUMMY )
             continue;
+
+        /* Validate the vcpu_id in the response. */
+        if ( (rsp.vcpu_id >= d->max_vcpus) || !d->vcpu[rsp.vcpu_id] )
+            continue;
+
+        v = d->vcpu[rsp.vcpu_id];
+
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(d->vcpu[rsp.vcpu_id]);
+            vcpu_unpause(v);
     }
 }
 
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] Xen/mem_event: Validate the response vcpu_id before acting on it.

Quoted 5 lines · Andrew Cooper
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Andres Lagar-Cavilla <[email protected]>
Quoted 85 lines
---
 xen/arch/x86/mm/mem_sharing.c |   11 ++++++++++-
 xen/arch/x86/mm/p2m.c         |   22 ++++++++++++++++++++--
 2 files changed, 30 insertions(+), 3 deletions(-)
xen/arch/x86/mm/mem_sharing.c +10 -1
index 7293f31..ec99266 100644
@@ -596,11 +596,20 @@ int mem_sharing_sharing_resume(struct domain *d)
     /* Get all requests off the ring */
     while ( mem_event_get_response(d, &d->mem_event->share, &rsp) )
     {
+        struct vcpu *v;
+
         if ( rsp.flags & MEM_EVENT_FLAG_DUMMY )
             continue;
+
+        /* Validate the vcpu_id in the response. */
+        if ( (rsp.vcpu_id >= d->max_vcpus) || !d->vcpu[rsp.vcpu_id] )
+            continue;
+
+        v = d->vcpu[rsp.vcpu_id];
+
         /* Unpause domain/vcpu */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(d->vcpu[rsp.vcpu_id]);
+            vcpu_unpause(v);
     }
 
     return 0;
xen/arch/x86/mm/p2m.c +20 -3
index 642ec28..f213a39 100644
@@ -1290,8 +1290,17 @@ void p2m_mem_paging_resume(struct domain *d)
     /* Pull all responses off the ring */
     while( mem_event_get_response(d, &d->mem_event->paging, &rsp) )
     {
+        struct vcpu *v;
+
         if ( rsp.flags & MEM_EVENT_FLAG_DUMMY )
             continue;
+
+        /* Validate the vcpu_id in the response. */
+        if ( (rsp.vcpu_id >= d->max_vcpus) || !d->vcpu[rsp.vcpu_id] )
+            continue;
+
+        v = d->vcpu[rsp.vcpu_id];
+
         /* Fix p2m entry if the page was not dropped */
         if ( !(rsp.flags & MEM_EVENT_FLAG_DROP_PAGE) )
         {
@@ -1310,7 +1319,7 @@ void p2m_mem_paging_resume(struct domain *d)
         }
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(d->vcpu[rsp.vcpu_id]);
+            vcpu_unpause(v);
     }
 }
 
@@ -1418,11 +1427,20 @@ void p2m_mem_access_resume(struct domain *d)
     /* Pull all responses off the ring */
     while( mem_event_get_response(d, &d->mem_event->access, &rsp) )
     {
+        struct vcpu *v;
+
         if ( rsp.flags & MEM_EVENT_FLAG_DUMMY )
             continue;
+
+        /* Validate the vcpu_id in the response. */
+        if ( (rsp.vcpu_id >= d->max_vcpus) || !d->vcpu[rsp.vcpu_id] )
+            continue;
+
+        v = d->vcpu[rsp.vcpu_id];
+
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(d->vcpu[rsp.vcpu_id]);
+            vcpu_unpause(v);
     }
 }
 
--
Quoted 3 lines

1.7.10.4

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw

PATCH 2/2 Xen/mem_event: Prevent underflow of vcpu pause counts

In reply to Andrew Cooper
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Andres Lagar-Cavilla <[email protected]>
---
 xen/arch/x86/hvm/hvm.c          |    2 +-
 xen/arch/x86/mm/mem_event.c     |   14 ++++++++++++++
 xen/arch/x86/mm/mem_sharing.c   |    4 ++--
 xen/arch/x86/mm/p2m.c           |    8 ++++----
 xen/include/asm-x86/mem_event.h |    3 +++
 xen/include/xen/sched.h         |    2 ++
 6 files changed, 26 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/hvm.c +1 -1
index ef2411c..efd79b8 100644
@@ -6113,7 +6113,7 @@ static int hvm_memory_event_traps(long p, uint32_t reason,
     if ( (p & HVMPME_MODE_MASK) == HVMPME_mode_sync ) 
     {
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;    
-        vcpu_pause_nosync(v);   
+        mem_event_vcpu_pause(v);
     }
 
     req.gfn = value;
xen/arch/x86/mm/mem_event.c +14 -0
index 40ae841..ef5197c 100644
@@ -663,6 +663,20 @@ int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
     return rc;
 }
 
+void mem_event_vcpu_pause(struct vcpu *v)
+{
+    ASSERT(v == current);
+
+    /* Ensure we don't try to repeatedly pause the same vcpu. */
+    BUG_ON(test_and_set_bool(v->paused_for_mem_event) != 0);
+    vcpu_pause_nosync(v);
+}
+
+void mem_event_vcpu_unpause(struct vcpu *v)
+{
+    if ( test_and_clear_bool(v->paused_for_mem_event) )
+        vcpu_unpause(v);
+}
 
 /*
  * Local variables:
xen/arch/x86/mm/mem_sharing.c +2 -2
index ec99266..79188b9 100644
@@ -568,7 +568,7 @@ int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
     if ( v->domain == d )
     {
         req.flags = MEM_EVENT_FLAG_VCPU_PAUSED;
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
     }
 
     req.p2mt = p2m_ram_shared;
@@ -609,7 +609,7 @@ int mem_sharing_sharing_resume(struct domain *d)
 
         /* Unpause domain/vcpu */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 
     return 0;
xen/arch/x86/mm/p2m.c +4 -4
index f213a39..2c7bc0f 100644
@@ -1158,7 +1158,7 @@ void p2m_mem_paging_populate(struct domain *d, unsigned long gfn)
     /* Pause domain if request came from guest and gfn has paging type */
     if ( p2m_is_paging(p2mt) && v->domain == d )
     {
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
     }
     /* No need to inform pager if the gfn is not in the page-out path */
@@ -1319,7 +1319,7 @@ void p2m_mem_paging_resume(struct domain *d)
         }
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
@@ -1414,7 +1414,7 @@ bool_t p2m_mem_access_check(paddr_t gpa, bool_t gla_valid, unsigned long gla,
 
     /* Pause the current VCPU */
     if ( p2ma != p2m_access_n2rwx )
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
 
     /* VCPU may be paused, return whether we promoted automatically */
     return (p2ma == p2m_access_n2rwx);
@@ -1440,7 +1440,7 @@ void p2m_mem_access_resume(struct domain *d)
 
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
xen/include/asm-x86/mem_event.h +3 -0
index 045ef9b..ed4481a 100644
@@ -66,6 +66,9 @@ int do_mem_event_op(int op, uint32_t domain, void *arg);
 int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
                      XEN_GUEST_HANDLE_PARAM(void) u_domctl);
 
+void mem_event_vcpu_pause(struct vcpu *v);
+void mem_event_vcpu_unpause(struct vcpu *v);
+
 #endif /* __MEM_EVENT_H__ */
 
 
xen/include/xen/sched.h +2 -0
index 2f876f5..ea33b7d 100644
@@ -199,6 +199,8 @@ struct vcpu
     bool_t           paused_for_shutdown;
     /* VCPU need affinity restored */
     bool_t           affinity_broken;
+    /* VCPU paused for mem event reply. */
+    bool_t           paused_for_mem_event;
 
 
     /*
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [PATCH 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

Quoted 5 lines · Andrew Cooper
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Andres Lagar-Cavilla <[email protected]>

Not so sure about this one, see below.

Quoted 29 lines
---
 xen/arch/x86/hvm/hvm.c          |    2 +-
 xen/arch/x86/mm/mem_event.c     |   14 ++++++++++++++
 xen/arch/x86/mm/mem_sharing.c   |    4 ++--
 xen/arch/x86/mm/p2m.c           |    8 ++++----
 xen/include/asm-x86/mem_event.h |    3 +++
 xen/include/xen/sched.h         |    2 ++
 6 files changed, 26 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/hvm.c +1 -1
index ef2411c..efd79b8 100644
@@ -6113,7 +6113,7 @@ static int hvm_memory_event_traps(long p, uint32_t
 reason,
     if ( (p & HVMPME_MODE_MASK) == HVMPME_mode_sync )
     {
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
     }
 
     req.gfn = value;
xen/arch/x86/mm/mem_event.c +0 -0
index 40ae841..ef5197c 100644
@@ -663,6 +663,20 @@ int mem_event_domctl(struct domain *d,
 xen_domctl_mem_event_op_t *mec,

xen/arch/x86/mm/mem_event.c int mem_event_domctl(struct domain *d, · line 673

Quoted 10 lines
xen/arch/x86/mm/mem_event.c +6 -0
     return rc;
 }
 
+void mem_event_vcpu_pause(struct vcpu *v)
+{
+    ASSERT(v == current);
+
+    /* Ensure we don't try to repeatedly pause the same vcpu. */
+    BUG_ON(test_and_set_bool(v->paused_for_mem_event) != 0);
 

This is a problem. It relies on a vcpu being able to cause a single mem event during a vmexit. I don't think that can be guaranteed. While I can't pinpoint the exact conversation from years ago, it is not hard to imagine scenarios in which an mmio emulation can touch multiple pages. So I don't think we can BUG_ON at all.

xen/arch/x86/mm/mem_event.c int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec, · line 680

Quoted 7 lines
xen/arch/x86/mm/mem_event.c +6 -0
+    vcpu_pause_nosync(v);
+}
+
+void mem_event_vcpu_unpause(struct vcpu *v)
+{
+    if ( test_and_clear_bool(v->paused_for_mem_event) )
 

And now that we consider more than one mem event piling up to pause a vcpu, this has to become an atomic counter, which unpauses on zero, and takes care of underflow.

This is great and thanks for catching these things. Andres

Quoted 99 lines
xen/arch/x86/mm/mem_event.c +2 -0
+        vcpu_unpause(v);
+}

/*
* Local variables:

xen/arch/x86/mm/mem_sharing.c +1 -1
index ec99266..79188b9 100644
@@ -568,7 +568,7 @@ int mem_sharing_notify_enomem(struct domain *d,
 unsigned long gfn,
     if ( v->domain == d )
     {
         req.flags = MEM_EVENT_FLAG_VCPU_PAUSED;
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
     }
 
     req.p2mt = p2m_ram_shared;
xen/arch/x86/mm/mem_sharing.c +1 -1
@@ -609,7 +609,7 @@ int mem_sharing_sharing_resume(struct domain *d)
 
         /* Unpause domain/vcpu */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 
     return 0;
xen/arch/x86/mm/p2m.c +1 -1
index f213a39..2c7bc0f 100644
@@ -1158,7 +1158,7 @@ void p2m_mem_paging_populate(struct domain *d,
 unsigned long gfn)
     /* Pause domain if request came from guest and gfn has paging type */
     if ( p2m_is_paging(p2mt) && v->domain == d )
     {
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
     }
     /* No need to inform pager if the gfn is not in the page-out path */
xen/arch/x86/mm/p2m.c +2 -2
@@ -1319,7 +1319,7 @@ void p2m_mem_paging_resume(struct domain *d)
         }
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
@@ -1414,7 +1414,7 @@ bool_t p2m_mem_access_check(paddr_t gpa, bool_t
 gla_valid, unsigned long gla,
 
     /* Pause the current VCPU */
     if ( p2ma != p2m_access_n2rwx )
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
 
     /* VCPU may be paused, return whether we promoted automatically */
     return (p2ma == p2m_access_n2rwx);
xen/arch/x86/mm/p2m.c +1 -1
@@ -1440,7 +1440,7 @@ void p2m_mem_access_resume(struct domain *d)
 
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
xen/include/asm-x86/mem_event.h +3 -0
index 045ef9b..ed4481a 100644
@@ -66,6 +66,9 @@ int do_mem_event_op(int op, uint32_t domain, void *arg);
 int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
                      XEN_GUEST_HANDLE_PARAM(void) u_domctl);
 
+void mem_event_vcpu_pause(struct vcpu *v);
+void mem_event_vcpu_unpause(struct vcpu *v);
+
 #endif /* __MEM_EVENT_H__ */
 
 
xen/include/xen/sched.h +2 -1
index 2f876f5..ea33b7d 100644
@@ -199,6 +199,8 @@ struct vcpu
     bool_t           paused_for_shutdown;
     /* VCPU need affinity restored */
     bool_t           affinity_broken;
+    /* VCPU paused for mem event reply. */
+    bool_t           paused_for_mem_event;
 
 
     /*
--
Quoted 3 lines

1.7.10.4

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

A message that is not in the archive (sent off-list or not yet received): CAGU+auv8zMj+xqU8KhbQSZXM+J+HovjV=TZMab5Z+nzNCvpjaQ@mail.gmail.com

#6 lore raw

Re: [PATCH 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+void mem_event_vcpu_unpause(struct vcpu *v) {
+    if ( test_and_clear_bool(v->paused_for_mem_event) )

And now that we consider more than one mem event piling up to pause a vcpu, this has to become an atomic counter, which unpauses on zero, and takes care of underflow.

Very true. I have seen this event pile up occur in practice in our product.

Thanks,
Aravindh

#7 lore raw

Re: [PATCH 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

Quoted 9 lines · Aravindh Puthiyaparambil (aravindp)
Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+void mem_event_vcpu_unpause(struct vcpu *v) {
+    if ( test_and_clear_bool(v->paused_for_mem_event) )

And now that we consider more than one mem event piling up to pause a vcpu, this has to become an atomic counter, which unpauses on zero, and takes care of underflow.

Very true. I have seen this event pile up occur in practice in our product.

The problem becomes how to tell apart real event responses that should dec the pause count from spurious crap from the toolstack. IOW, how to not unpause the vcpu when count reaches zero due to bad responses. I think the answer is: you can't, if the toolstack is evil, behavior undefined and bigger fish to fry.

Andres

Quoted 5 lines

Thanks,
Aravindh

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#8 lore raw

Re: [PATCH 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+void mem_event_vcpu_unpause(struct vcpu *v) {
+    if ( test_and_clear_bool(v->paused_for_mem_event) )

And now that we consider more than one mem event piling up to pause a vcpu, this has to become an atomic counter, which unpauses on zero, and takes care of underflow.

Very true. I have seen this event pile up occur in practice in our product.
The problem becomes how to tell apart real event responses that should dec the pause count from spurious crap from the toolstack. IOW, how to not unpause the vcpu when count reaches zero due to bad responses. I think the answer is: you can't, if the toolstack is evil, behavior undefined and bigger fish to fry.

Would that be a problem? AFAIK, you can have only one mem_event listener for a domain at a time.

Aravindh

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

Re: [PATCH 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

In reply to Andres Lagar Cavilla
Quoted 14 lines · Andres Lagar Cavilla
Quoted 5 lines · On Thu, Jul 17, 2014 at 2:51 PM, Aravindh Puthiyaparambil (aravindp)…

Review

Quoted 2 lines
(quoted diff) +2 -0
+void mem_event_vcpu_unpause(struct vcpu *v) {
+    if ( test_and_clear_bool(v->paused_for_mem_event) )

And now that we consider more than one mem event piling up to pause a vcpu, this has to become an atomic counter, which unpauses on

zero, and
>takes care of underflow.

Very true. I have seen this event pile up occur in practice in our product.

Quoted 7 lines

The problem becomes how to tell apart real event responses that should dec the pause count from spurious crap from the toolstack. IOW, how to not unpause the vcpu when count reaches zero due to bad responses. I think the answer is: you can't, if the toolstack is evil, behavior undefined and bigger fish to fry.

Andres

You really can't, but the important bit is to ensure that Xen is sufficiently insulated from buggy toolstack components that it doesn't fall over.

Quoted 1 line

From my experimenting with the pausedomain refcoutnging, weird stuff

happens when the domain pause count turns negative. I ended up with a domain which would never be scheduled again (even after returning the count to positive and back to 0), and a domain which couldn't be killed using xl destroy. Rebooting was the only option.

So long as Xen doesn't fall into these problems, a buggy toolstack (especially with mem_events) already has many ways to screw over a domain, so one more is not a problem.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#10 lore raw

Re: [PATCH 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

Quoted 6 lines · On Thu, Jul 17, 2014 at 2:51 PM, Aravindh Puthiyaparambil (aravindp)…

Review

Quoted 2 lines
(quoted diff) +2 -0
+void mem_event_vcpu_unpause(struct vcpu *v) {
+    if ( test_and_clear_bool(v->paused_for_mem_event) )

And now that we consider more than one mem event piling up to pause a vcpu, this has to become an atomic counter, which unpauses on zero, and takes care of underflow.

Very true. I have seen this event pile up occur in practice in our product.
The problem becomes how to tell apart real event responses that should dec the pause count from spurious crap from the toolstack. IOW, how to not unpause the vcpu when count reaches zero due to bad responses. I think the answer is: you can't, if the toolstack is evil, behavior undefined and bigger fish to fry.

Andres

You really can't, but the important bit is to ensure that Xen is sufficiently insulated from buggy toolstack components that it doesn't fall over.

From my experimenting with the pausedomain refcoutnging, weird stuff happens when the domain pause count turns negative. I ended up with a domain which would never be scheduled again (even after returning the count to positive and back to 0), and a domain which couldn't be killed using xl destroy. Rebooting was the only option.

So long as Xen doesn't fall into these problems, a buggy toolstack (especially with mem_events) already has many ways to screw over a domain, so one more is not a problem.

I misunderstood Andre’s point. Your response made it clear what the concern was.

Thanks,
Aravindh

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#11 lore raw

Re: [PATCH 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

In reply to Andres Lagar Cavilla
Quoted 51 lines · Andres Lagar Cavilla

On Thu, Jul 17, 2014 at 9:10 AM, Andrew Cooper <[email protected] <mailto:[email protected]>> wrote:

<mailto:[email protected]>>
CC: Jan Beulich <[email protected] <mailto:[email protected]>>
CC: Tim Deegan <[email protected] <mailto:[email protected]>>
CC: Andres Lagar-Cavilla <[email protected] <mailto:[email protected]>>

Not so sure about this one, see below.

    ---
     xen/arch/x86/hvm/hvm.c          |    2 +-
     xen/arch/x86/mm/mem_event.c     |   14 ++++++++++++++
     xen/arch/x86/mm/mem_sharing.c   |    4 ++--
     xen/arch/x86/mm/p2m.c           |    8 ++++----
     xen/include/asm-x86/mem_event.h |    3 +++
     xen/include/xen/sched.h         |    2 ++
     6 files changed, 26 insertions(+), 7 deletions(-)

    diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
    index ef2411c..efd79b8 100644
    --- a/xen/arch/x86/hvm/hvm.c
    +++ b/xen/arch/x86/hvm/hvm.c
    @@ -6113,7 +6113,7 @@ static int hvm_memory_event_traps(long p,
    uint32_t reason,
         if ( (p & HVMPME_MODE_MASK) == HVMPME_mode_sync )
         {
             req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
    -        vcpu_pause_nosync(v);
    +        mem_event_vcpu_pause(v);
         }

         req.gfn = value;
    diff --git a/xen/arch/x86/mm/mem_event.c b/xen/arch/x86/mm/mem_event.c
    index 40ae841..ef5197c 100644
    --- a/xen/arch/x86/mm/mem_event.c
    +++ b/xen/arch/x86/mm/mem_event.c
    @@ -663,6 +663,20 @@ int mem_event_domctl(struct domain *d,
    xen_domctl_mem_event_op_t *mec,
         return rc;
     }

    +void mem_event_vcpu_pause(struct vcpu *v)
    +{
    +    ASSERT(v == current);
    +
    +    /* Ensure we don't try to repeatedly pause the same vcpu. */
    +    BUG_ON(test_and_set_bool(v->paused_for_mem_event) != 0);
Quoted 5 lines

This is a problem. It relies on a vcpu being able to cause a single mem event during a vmexit. I don't think that can be guaranteed. While I can't pinpoint the exact conversation from years ago, it is not hard to imagine scenarios in which an mmio emulation can touch multiple pages. So I don't think we can BUG_ON at all.

Hmm - Would the emulator not fail entirely after first fail only?

Still - it is probably better to accept multiple pause requests.

Quoted 10 lines

+ vcpu_pause_nosync(v);
+}
+
+void mem_event_vcpu_unpause(struct vcpu *v)
+{
+ if ( test_and_clear_bool(v->paused_for_mem_event) )

And now that we consider more than one mem event piling up to pause a vcpu, this has to become an atomic counter, which unpauses on zero, and takes care of underflow.

I shall spin a v2 with this problem addressed. I suspect it will look curiously similar to the DOMCTL_{,un}pausedomain refcounting fix which suffered from exactly the same problem wrt underflow/overflow of the pause count.

~Andrew

Quoted 102 lines

This is great and thanks for catching these things. Andres

    +        vcpu_unpause(v);
    +}

     /*
      * Local variables:
    diff --git a/xen/arch/x86/mm/mem_sharing.c
    b/xen/arch/x86/mm/mem_sharing.c
    index ec99266..79188b9 100644
    --- a/xen/arch/x86/mm/mem_sharing.c
    +++ b/xen/arch/x86/mm/mem_sharing.c
    @@ -568,7 +568,7 @@ int mem_sharing_notify_enomem(struct domain
    *d, unsigned long gfn,
         if ( v->domain == d )
         {
             req.flags = MEM_EVENT_FLAG_VCPU_PAUSED;
    -        vcpu_pause_nosync(v);
    +        mem_event_vcpu_pause(v);
         }

         req.p2mt = p2m_ram_shared;
    @@ -609,7 +609,7 @@ int mem_sharing_sharing_resume(struct domain *d)

             /* Unpause domain/vcpu */
             if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
    -            vcpu_unpause(v);
    +            mem_event_vcpu_unpause(v);
         }

         return 0;
    diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c
    index f213a39..2c7bc0f 100644
    --- a/xen/arch/x86/mm/p2m.c
    +++ b/xen/arch/x86/mm/p2m.c
    @@ -1158,7 +1158,7 @@ void p2m_mem_paging_populate(struct domain
    *d, unsigned long gfn)
         /* Pause domain if request came from guest and gfn has paging
    type */
         if ( p2m_is_paging(p2mt) && v->domain == d )
         {
    -        vcpu_pause_nosync(v);
    +        mem_event_vcpu_pause(v);
             req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
         }
         /* No need to inform pager if the gfn is not in the page-out
    path */
    @@ -1319,7 +1319,7 @@ void p2m_mem_paging_resume(struct domain *d)
             }
             /* Unpause domain */
             if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
    -            vcpu_unpause(v);
    +            mem_event_vcpu_unpause(v);
         }
     }

    @@ -1414,7 +1414,7 @@ bool_t p2m_mem_access_check(paddr_t gpa,
    bool_t gla_valid, unsigned long gla,

         /* Pause the current VCPU */
         if ( p2ma != p2m_access_n2rwx )
    -        vcpu_pause_nosync(v);
    +        mem_event_vcpu_pause(v);

         /* VCPU may be paused, return whether we promoted
    automatically */
         return (p2ma == p2m_access_n2rwx);
    @@ -1440,7 +1440,7 @@ void p2m_mem_access_resume(struct domain *d)

             /* Unpause domain */
             if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
    -            vcpu_unpause(v);
    +            mem_event_vcpu_unpause(v);
         }
     }

    diff --git a/xen/include/asm-x86/mem_event.h
    b/xen/include/asm-x86/mem_event.h
    index 045ef9b..ed4481a 100644
    --- a/xen/include/asm-x86/mem_event.h
    +++ b/xen/include/asm-x86/mem_event.h
    @@ -66,6 +66,9 @@ int do_mem_event_op(int op, uint32_t domain,
    void *arg);
     int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t
    *mec,
                          XEN_GUEST_HANDLE_PARAM(void) u_domctl);

    +void mem_event_vcpu_pause(struct vcpu *v);
    +void mem_event_vcpu_unpause(struct vcpu *v);
    +
     #endif /* __MEM_EVENT_H__ */


    diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
    index 2f876f5..ea33b7d 100644
    --- a/xen/include/xen/sched.h
    +++ b/xen/include/xen/sched.h
    @@ -199,6 +199,8 @@ struct vcpu
         bool_t           paused_for_shutdown;
         /* VCPU need affinity restored */
Quoted 10 lines
         bool_t           affinity_broken;
    +    /* VCPU paused for mem event reply. */
    +    bool_t           paused_for_mem_event;


         /*
    --
    1.7.10.4

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#12 lore raw

Re: [PATCH 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

In reply to Andres Lagar Cavilla
Quoted 6 lines · Andres Lagar Cavilla

+ BUG_ON(test_and_set_bool(v->paused_for_mem_event) != 0);
This is a problem. It relies on a vcpu being able to cause a single mem event during a vmexit. I don't think that can be guaranteed. While I can't pinpoint the exact conversation from years ago, it is not hard to imagine scenarios in which an mmio emulation can touch multiple pages.

Since some x86 instructions can have both its input and output in memory it's not hard to imagine both being MMIO, at least if you were trying to break things...

Ian.

#13 lore raw

PATCH v2 2/2 Xen/mem_event: Prevent underflow of vcpu pause counts

In reply to Andrew Cooper
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Andres Lagar-Cavilla <[email protected]>
CC Razvan Cojocaru <[email protected]>
CC Aravindh Puthiyaparambil <[email protected]>

--
v2:
* Allow for multiple pause refcounts

---
 xen/arch/x86/hvm/hvm.c          |    2 +-
 xen/arch/x86/mm/mem_event.c     |   31 +++++++++++++++++++++++++++++++
 xen/arch/x86/mm/mem_sharing.c   |    4 ++--
 xen/arch/x86/mm/p2m.c           |    8 ++++----
 xen/include/asm-x86/mem_event.h |    3 +++
 xen/include/xen/sched.h         |    3 +++
 6 files changed, 44 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/hvm.c +1 -1
index ef2411c..efd79b8 100644
@@ -6113,7 +6113,7 @@ static int hvm_memory_event_traps(long p, uint32_t reason,
     if ( (p & HVMPME_MODE_MASK) == HVMPME_mode_sync ) 
     {
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;    
-        vcpu_pause_nosync(v);   
+        mem_event_vcpu_pause(v);
     }
 
     req.gfn = value;
xen/arch/x86/mm/mem_event.c +31 -0
index 40ae841..bdde426 100644
@@ -663,6 +663,37 @@ int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
     return rc;
 }
 
+void mem_event_vcpu_pause(struct vcpu *v)
+{
+    ASSERT(v == current);
+
+    atomic_inc(&v->mem_event_pause_count);
+    vcpu_pause_nosync(v);
+}
+
+void mem_event_vcpu_unpause(struct vcpu *v)
+{
+    int old, new, prev = v->mem_event_pause_count.counter;
+
+    /* All unpause requests as a result of toolstack responses.  Prevent
+     * underflow of the vcpu pause count. */
+    do
+    {
+        old = prev;
+        new = old - 1;
+
+        if ( new < 0 )
+        {
+            printk(XENLOG_G_WARNING
+                   "%pv mem_event: Too many unpause attempts", v);
+            return;
+        }
+
+        prev = cmpxchg(&v->mem_event_pause_count.counter, old, new);
+    } while ( prev != old );
+
+    vcpu_unpause(v);
+}
 
 /*
  * Local variables:
xen/arch/x86/mm/mem_sharing.c +2 -2
index ec99266..79188b9 100644
@@ -568,7 +568,7 @@ int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
     if ( v->domain == d )
     {
         req.flags = MEM_EVENT_FLAG_VCPU_PAUSED;
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
     }
 
     req.p2mt = p2m_ram_shared;
@@ -609,7 +609,7 @@ int mem_sharing_sharing_resume(struct domain *d)
 
         /* Unpause domain/vcpu */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 
     return 0;
xen/arch/x86/mm/p2m.c +4 -4
index f213a39..2c7bc0f 100644
@@ -1158,7 +1158,7 @@ void p2m_mem_paging_populate(struct domain *d, unsigned long gfn)
     /* Pause domain if request came from guest and gfn has paging type */
     if ( p2m_is_paging(p2mt) && v->domain == d )
     {
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
     }
     /* No need to inform pager if the gfn is not in the page-out path */
@@ -1319,7 +1319,7 @@ void p2m_mem_paging_resume(struct domain *d)
         }
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
@@ -1414,7 +1414,7 @@ bool_t p2m_mem_access_check(paddr_t gpa, bool_t gla_valid, unsigned long gla,
 
     /* Pause the current VCPU */
     if ( p2ma != p2m_access_n2rwx )
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
 
     /* VCPU may be paused, return whether we promoted automatically */
     return (p2ma == p2m_access_n2rwx);
@@ -1440,7 +1440,7 @@ void p2m_mem_access_resume(struct domain *d)
 
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
xen/include/asm-x86/mem_event.h +3 -0
index 045ef9b..ed4481a 100644
@@ -66,6 +66,9 @@ int do_mem_event_op(int op, uint32_t domain, void *arg);
 int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
                      XEN_GUEST_HANDLE_PARAM(void) u_domctl);
 
+void mem_event_vcpu_pause(struct vcpu *v);
+void mem_event_vcpu_unpause(struct vcpu *v);
+
 #endif /* __MEM_EVENT_H__ */
 
 
xen/include/xen/sched.h +3 -0
index 2f876f5..62a4785 100644
@@ -214,6 +214,9 @@ struct vcpu
     unsigned long    pause_flags;
     atomic_t         pause_count;
 
+    /* VCPU paused for mem_event replies. */
+    atomic_t         mem_event_pause_count;
+
     /* IRQ-safe virq_lock protects against delivering VIRQ to stale evtchn. */
     evtchn_port_t    virq_to_evtchn[NR_VIRQS];
     spinlock_t       virq_lock;
Signature · 1 line
1.7.10.4
Tested-by on v2 2/2
#14 lore raw

Re: [PATCH v2 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

Quoted 10 lines · Andrew Cooper
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Andres Lagar-Cavilla <[email protected]>
CC Razvan Cojocaru <[email protected]>
CC Aravindh Puthiyaparambil <[email protected]>

--
v2:
* Allow for multiple pause refcounts

Quoted 8 lines
---
 xen/arch/x86/hvm/hvm.c          |    2 +-
 xen/arch/x86/mm/mem_event.c     |   31 +++++++++++++++++++++++++++++++
 xen/arch/x86/mm/mem_sharing.c   |    4 ++--
 xen/arch/x86/mm/p2m.c           |    8 ++++----
 xen/include/asm-x86/mem_event.h |    3 +++
 xen/include/xen/sched.h         |    3 +++
 6 files changed, 44 insertions(+), 7 deletions(-)

Thanks,
Razvan Cojocaru

#15 lore raw

PATCH v3 2/2 Xen/mem_event: Prevent underflow of vcpu pause counts

In reply to Andrew Cooper
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Andres Lagar-Cavilla <[email protected]>
CC Aravindh Puthiyaparambil <[email protected]>

--
v3:
* Newline on warning
v2:
* Allow for multiple pause refcounts

---
 xen/arch/x86/hvm/hvm.c          |    2 +-
 xen/arch/x86/mm/mem_event.c     |   31 +++++++++++++++++++++++++++++++
 xen/arch/x86/mm/mem_sharing.c   |    4 ++--
 xen/arch/x86/mm/p2m.c           |    8 ++++----
 xen/include/asm-x86/mem_event.h |    3 +++
 xen/include/xen/sched.h         |    3 +++
 6 files changed, 44 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/hvm.c +1 -1
index ef2411c..efd79b8 100644
@@ -6113,7 +6113,7 @@ static int hvm_memory_event_traps(long p, uint32_t reason,
     if ( (p & HVMPME_MODE_MASK) == HVMPME_mode_sync ) 
     {
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;    
-        vcpu_pause_nosync(v);   
+        mem_event_vcpu_pause(v);
     }
 
     req.gfn = value;
xen/arch/x86/mm/mem_event.c +31 -0
index 40ae841..ba7e71e 100644
@@ -663,6 +663,37 @@ int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
     return rc;
 }
 
+void mem_event_vcpu_pause(struct vcpu *v)
+{
+    ASSERT(v == current);
+
+    atomic_inc(&v->mem_event_pause_count);
+    vcpu_pause_nosync(v);
+}
+
+void mem_event_vcpu_unpause(struct vcpu *v)
+{
+    int old, new, prev = v->mem_event_pause_count.counter;
+
+    /* All unpause requests as a result of toolstack responses.  Prevent
+     * underflow of the vcpu pause count. */
+    do
+    {
+        old = prev;
+        new = old - 1;
+
+        if ( new < 0 )
+        {
+            printk(XENLOG_G_WARNING
+                   "%pv mem_event: Too many unpause attempts\n", v);
+            return;
+        }
+
+        prev = cmpxchg(&v->mem_event_pause_count.counter, old, new);
+    } while ( prev != old );
+
+    vcpu_unpause(v);
+}
 
 /*
  * Local variables:
xen/arch/x86/mm/mem_sharing.c +2 -2
index ec99266..79188b9 100644
@@ -568,7 +568,7 @@ int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
     if ( v->domain == d )
     {
         req.flags = MEM_EVENT_FLAG_VCPU_PAUSED;
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
     }
 
     req.p2mt = p2m_ram_shared;
@@ -609,7 +609,7 @@ int mem_sharing_sharing_resume(struct domain *d)
 
         /* Unpause domain/vcpu */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 
     return 0;
xen/arch/x86/mm/p2m.c +4 -4
index f213a39..2c7bc0f 100644
@@ -1158,7 +1158,7 @@ void p2m_mem_paging_populate(struct domain *d, unsigned long gfn)
     /* Pause domain if request came from guest and gfn has paging type */
     if ( p2m_is_paging(p2mt) && v->domain == d )
     {
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
     }
     /* No need to inform pager if the gfn is not in the page-out path */
@@ -1319,7 +1319,7 @@ void p2m_mem_paging_resume(struct domain *d)
         }
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
@@ -1414,7 +1414,7 @@ bool_t p2m_mem_access_check(paddr_t gpa, bool_t gla_valid, unsigned long gla,
 
     /* Pause the current VCPU */
     if ( p2ma != p2m_access_n2rwx )
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
 
     /* VCPU may be paused, return whether we promoted automatically */
     return (p2ma == p2m_access_n2rwx);
@@ -1440,7 +1440,7 @@ void p2m_mem_access_resume(struct domain *d)
 
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
xen/include/asm-x86/mem_event.h +3 -0
index 045ef9b..ed4481a 100644
@@ -66,6 +66,9 @@ int do_mem_event_op(int op, uint32_t domain, void *arg);
 int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
                      XEN_GUEST_HANDLE_PARAM(void) u_domctl);
 
+void mem_event_vcpu_pause(struct vcpu *v);
+void mem_event_vcpu_unpause(struct vcpu *v);
+
 #endif /* __MEM_EVENT_H__ */
 
 
xen/include/xen/sched.h +3 -0
index 2f876f5..62a4785 100644
@@ -214,6 +214,9 @@ struct vcpu
     unsigned long    pause_flags;
     atomic_t         pause_count;
 
+    /* VCPU paused for mem_event replies. */
+    atomic_t         mem_event_pause_count;
+
     /* IRQ-safe virq_lock protects against delivering VIRQ to stale evtchn. */
     evtchn_port_t    virq_to_evtchn[NR_VIRQS];
     spinlock_t       virq_lock;
Signature · 1 line
1.7.10.4
Reviewed-by on v3 2/2
#16 lore raw

Re: [PATCH v3 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

Quoted 7 lines · Andrew Cooper
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Andres Lagar-Cavilla <[email protected]>
CC Aravindh Puthiyaparambil <[email protected]>

Couple nits below ...

Quoted 34 lines

--
v3:
* Newline on warning
v2:
* Allow for multiple pause refcounts

---
 xen/arch/x86/hvm/hvm.c          |    2 +-
 xen/arch/x86/mm/mem_event.c     |   31 +++++++++++++++++++++++++++++++
 xen/arch/x86/mm/mem_sharing.c   |    4 ++--
 xen/arch/x86/mm/p2m.c           |    8 ++++----
 xen/include/asm-x86/mem_event.h |    3 +++
 xen/include/xen/sched.h         |    3 +++
 6 files changed, 44 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/hvm.c +1 -1
index ef2411c..efd79b8 100644
@@ -6113,7 +6113,7 @@ static int hvm_memory_event_traps(long p, uint32_t
 reason,
     if ( (p & HVMPME_MODE_MASK) == HVMPME_mode_sync )
     {
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
     }
 
     req.gfn = value;
xen/arch/x86/mm/mem_event.c +0 -0
index 40ae841..ba7e71e 100644

xen/arch/x86/mm/mem_event.c int mem_event_domctl(struct domain *d, · line 672

Quoted 10 lines
xen/arch/x86/mm/mem_event.c +5 -0
@@ -663,6 +663,37 @@ int mem_event_domctl(struct domain *d,
 xen_domctl_mem_event_op_t *mec,
     return rc;
 }
 
+void mem_event_vcpu_pause(struct vcpu *v)
+{
+    ASSERT(v == current);
+
+    atomic_inc(&v->mem_event_pause_count);
 

Nit #1: A buggy toolstack going into an infinite loop could overflow this.

xen/arch/x86/mm/mem_event.c int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec, · line 697

Quoted 7 lines
xen/arch/x86/mm/mem_event.c +6 -0
+    vcpu_pause_nosync(v);
+}
+
+void mem_event_vcpu_unpause(struct vcpu *v)
+{
+    int old, new, prev = v->mem_event_pause_count.counter;
 

Nit #2: not fresh in my mind whether atomic is supposed to be signed or unsigned. The flawless comparison below is to take these all as unsigned and check for new > old.

Thanks
Andres

Quoted 116 lines
xen/arch/x86/mm/mem_event.c +20 -0
+
+    /* All unpause requests as a result of toolstack responses.  Prevent
+     * underflow of the vcpu pause count. */
+    do
+    {
+        old = prev;
+        new = old - 1;
+
+        if ( new < 0 )
+        {
+            printk(XENLOG_G_WARNING
+                   "%pv mem_event: Too many unpause attempts\n", v);
+            return;
+        }
+
+        prev = cmpxchg(&v->mem_event_pause_count.counter, old, new);
+    } while ( prev != old );
+
+    vcpu_unpause(v);
+}

/*
* Local variables:

xen/arch/x86/mm/mem_sharing.c +1 -1
index ec99266..79188b9 100644
@@ -568,7 +568,7 @@ int mem_sharing_notify_enomem(struct domain *d,
 unsigned long gfn,
     if ( v->domain == d )
     {
         req.flags = MEM_EVENT_FLAG_VCPU_PAUSED;
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
     }
 
     req.p2mt = p2m_ram_shared;
xen/arch/x86/mm/mem_sharing.c +1 -1
@@ -609,7 +609,7 @@ int mem_sharing_sharing_resume(struct domain *d)
 
         /* Unpause domain/vcpu */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 
     return 0;
xen/arch/x86/mm/p2m.c +1 -1
index f213a39..2c7bc0f 100644
@@ -1158,7 +1158,7 @@ void p2m_mem_paging_populate(struct domain *d,
 unsigned long gfn)
     /* Pause domain if request came from guest and gfn has paging type */
     if ( p2m_is_paging(p2mt) && v->domain == d )
     {
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
         req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
     }
     /* No need to inform pager if the gfn is not in the page-out path */
xen/arch/x86/mm/p2m.c +2 -2
@@ -1319,7 +1319,7 @@ void p2m_mem_paging_resume(struct domain *d)
         }
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
@@ -1414,7 +1414,7 @@ bool_t p2m_mem_access_check(paddr_t gpa, bool_t
 gla_valid, unsigned long gla,
 
     /* Pause the current VCPU */
     if ( p2ma != p2m_access_n2rwx )
-        vcpu_pause_nosync(v);
+        mem_event_vcpu_pause(v);
 
     /* VCPU may be paused, return whether we promoted automatically */
     return (p2ma == p2m_access_n2rwx);
xen/arch/x86/mm/p2m.c +1 -1
@@ -1440,7 +1440,7 @@ void p2m_mem_access_resume(struct domain *d)
 
         /* Unpause domain */
         if ( rsp.flags & MEM_EVENT_FLAG_VCPU_PAUSED )
-            vcpu_unpause(v);
+            mem_event_vcpu_unpause(v);
     }
 }
 
xen/include/asm-x86/mem_event.h +3 -0
index 045ef9b..ed4481a 100644
@@ -66,6 +66,9 @@ int do_mem_event_op(int op, uint32_t domain, void *arg);
 int mem_event_domctl(struct domain *d, xen_domctl_mem_event_op_t *mec,
                      XEN_GUEST_HANDLE_PARAM(void) u_domctl);
 
+void mem_event_vcpu_pause(struct vcpu *v);
+void mem_event_vcpu_unpause(struct vcpu *v);
+
 #endif /* __MEM_EVENT_H__ */
 
 
xen/include/xen/sched.h +3 -0
index 2f876f5..62a4785 100644
@@ -214,6 +214,9 @@ struct vcpu
     unsigned long    pause_flags;
     atomic_t         pause_count;
 
+    /* VCPU paused for mem_event replies. */
+    atomic_t         mem_event_pause_count;
+
     /* IRQ-safe virq_lock protects against delivering VIRQ to stale

evtchn. */

Quoted 6 lines
     evtchn_port_t    virq_to_evtchn[NR_VIRQS];
     spinlock_t       virq_lock;

--
1.7.10.4

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#17 lore raw

Re: [PATCH v3 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

Quoted 64 lines · Andres Lagar Cavilla

On Fri, Jul 18, 2014 at 9:53 AM, Andrew Cooper <[email protected] <mailto:[email protected]>> wrote:

<mailto:[email protected]>>

<mailto:[email protected]>>
CC: Jan Beulich <[email protected] <mailto:[email protected]>>
CC: Tim Deegan <[email protected] <mailto:[email protected]>>
CC: Andres Lagar-Cavilla <[email protected] <mailto:[email protected]>>
CC: Aravindh Puthiyaparambil <[email protected] <mailto:[email protected]>>

<mailto:[email protected]>>

Couple nits below ...

    --
    v3:
     * Newline on warning
    v2:
     * Allow for multiple pause refcounts
    ---
     xen/arch/x86/hvm/hvm.c          |    2 +-
     xen/arch/x86/mm/mem_event.c     |   31
    +++++++++++++++++++++++++++++++
     xen/arch/x86/mm/mem_sharing.c   |    4 ++--
     xen/arch/x86/mm/p2m.c           |    8 ++++----
     xen/include/asm-x86/mem_event.h |    3 +++
     xen/include/xen/sched.h         |    3 +++
     6 files changed, 44 insertions(+), 7 deletions(-)

    diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
    index ef2411c..efd79b8 100644
    --- a/xen/arch/x86/hvm/hvm.c
    +++ b/xen/arch/x86/hvm/hvm.c
    @@ -6113,7 +6113,7 @@ static int hvm_memory_event_traps(long p,
    uint32_t reason,
         if ( (p & HVMPME_MODE_MASK) == HVMPME_mode_sync )
         {
             req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
    -        vcpu_pause_nosync(v);
    +        mem_event_vcpu_pause(v);
         }

         req.gfn = value;
    diff --git a/xen/arch/x86/mm/mem_event.c b/xen/arch/x86/mm/mem_event.c
    index 40ae841..ba7e71e 100644
    --- a/xen/arch/x86/mm/mem_event.c
    +++ b/xen/arch/x86/mm/mem_event.c
    @@ -663,6 +663,37 @@ int mem_event_domctl(struct domain *d,
    xen_domctl_mem_event_op_t *mec,
         return rc;
     }

    +void mem_event_vcpu_pause(struct vcpu *v)
    +{
    +    ASSERT(v == current);
    +
    +    atomic_inc(&v->mem_event_pause_count);
Quoted 2 lines

Nit #1: A buggy toolstack going into an infinite loop could overflow this.

No it can't (or rather, I am fairly certain it can't). This is unconditionally in the context of a running vcpu, given the assertion above. This cannot increment the refcount more than number of mem_events triggered from a single exit into Xen.

Quoted 7 lines

+ vcpu_pause_nosync(v);
+}
+
+void mem_event_vcpu_unpause(struct vcpu *v)
+{
+ int old, new, prev = v->mem_event_pause_count.counter;

Quoted 6 lines

Nit #2: not fresh in my mind whether atomic is supposed to be signed or unsigned. The flawless comparison below is to take these all as unsigned and check for new > old.

Thanks
Andres

The inards of an atomic_t is signed.

Ideally I would make use of atomic_dec_bounded() from an early version of my pausedomain patch, but Jan requested that I drop that part for easier backport, given only a single consumer.

Given new consumers, that decision might be up for reconsideration.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#18 lore raw

Re: [PATCH v3 2/2] Xen/mem_event: Prevent underflow of vcpu pause counts

In reply to Andrew Cooper
Quoted 10 lines
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Andres Lagar-Cavilla <[email protected]>
CC Aravindh Puthiyaparambil <[email protected]>

--
v3:
* Newline on warning

Reviewed-by on the v1 series
#19 lore raw
In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

Xen performs insufficient validation of the contents of mem_event responses from the toolstack. As a result, a buggy toolstack could cause Xen to walk off the end of a domain's vcpu list, and get out of sync with vcpu pause reference counts.

These two fixes are compile tested only, as I have no way to plausibly test the mem-event functionality itself.

#20 lore raw
In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

Xen performs insufficient validation of the contents of mem_event responses from the toolstack. As a result, a buggy toolstack could cause Xen to walk off the end of a domain's vcpu list, and get out of sync with vcpu pause reference counts.

These two fixes are compile tested only, as I have no way to plausibly test the mem-event functionality itself.

I've ran a few tests for our application (heavily based on mem_event), and everything seems to work fine.

Hope this helps,
Razvan Cojocaru

#21 lore raw
Quoted 6 lines · Razvan Cojocaru
Quoted 5 lines · Andrew Cooper

Xen performs insufficient validation of the contents of mem_event responses from the toolstack. As a result, a buggy toolstack could cause Xen to walk off the end of a domain's vcpu list, and get out of sync with vcpu pause reference counts.

Quoted 8 lines
Quoted 2 lines

These two fixes are compile tested only, as I have no way to plausibly test the mem-event functionality itself.

I've ran a few tests for our application (heavily based on mem_event), and everything seems to work fine.

Hope this helps,
Razvan Cojocaru

Fantastic - thanks!

Does that mean you are happy for a "Tested-by: Razvan Cojocaru <[email protected]>" tag to accompany the series?

#22 lore raw
Quoted 13 lines · Andrew Cooper
Quoted 12 lines · Razvan Cojocaru
Quoted 7 lines · Andrew Cooper

Xen performs insufficient validation of the contents of mem_event responses from the toolstack. As a result, a buggy toolstack could cause Xen to walk off the end of a domain's vcpu list, and get out of sync with vcpu pause reference counts.

These two fixes are compile tested only, as I have no way to plausibly test the mem-event functionality itself.

I've ran a few tests for our application (heavily based on mem_event), and everything seems to work fine.

Quoted 7 lines
Quoted 2 lines

Hope this helps,
Razvan Cojocaru

Fantastic - thanks!

Does that mean you are happy for a "Tested-by: Razvan Cojocaru <[email protected]>" tag to accompany the series?

Sure, glad to be of service.Tested-by: Razvan Cojocaru <[email protected]>

A message that is not in the archive (sent off-list or not yet received): CAGU+auuzOr5HSErrxmyhtxtP74gn=0L5TAZGR8FWBF6MeGFxUA@mail.gmail.com

#24 lore raw
Quoted 7 lines

Xen performs insufficient validation of the contents of mem_event responses from the toolstack. As a result, a buggy toolstack could cause Xen to walk off the end of a domain's vcpu list, and get out of sync with vcpu pause reference counts.

These two fixes are compile tested only, as I have no way to plausibly test the mem-event functionality itself.

One easy way of testing is to use the tools/tests/xen-access test program which exercises mem_access and thereby mem_event. It is fairly easy to run. Bring up a domain and execute " xen-access <domain_id> write|exec". But I understand if you are under time constraints and cannot do it. If you Cc me on these patches, I will gladly test them for you.

Thanks,
Aravindh

#25 lore raw
Quoted 9 lines · Aravindh Puthiyaparambil (aravindp)
Quoted 7 lines

Xen performs insufficient validation of the contents of mem_event responses from the toolstack. As a result, a buggy toolstack could cause Xen to walk off the end of a domain's vcpu list, and get out of sync with vcpu pause reference counts.

These two fixes are compile tested only, as I have no way to plausibly test the mem-event functionality itself.

One easy way of testing is to use the tools/tests/xen-access test program which exercises mem_access and thereby mem_event. It is fairly easy to run. Bring up a domain and execute " xen-access <domain_id> write|exec". But I understand if you are under time constraints and cannot do it. If you Cc me on these patches, I will gladly test them for you.

Indeed, our application is very xen-access-like (except quite a bit more involved), and I've tested the original patches with 5 different domains 3 times over - but it's a well-behaved citizen of the Xen ecosystem and there were no gimmicks involved. No mem_events piled up, and there was always just one mem_event handler per domain.

Everything went without a hitch, but I did not try to pause the domain while it was running or try to trick the hypervisor in any way.

Thanks,
Razvan Cojocaru

#26 lore raw

I've also tested the patch with LibVMI and everything works fine. The pause/unpause reference count now does take effect, so the previous issue I reported (a paused domain getting unpaused by mem_event_enable) is fixed by this patch.

One question I have, what if the toolstack wants to unconditionally (force) unpause a domain? Right now with this patch if someone runs 'xl pause domain' a couple times he has no other recourse then to issue 'xl unpause domain' at least the same number of times, or to restart the entire domain. Might be user-friendlier if there was an override provided in case a domain got paused a million times by accident.

Cheers,
Tamas

On Thu, Jul 17, 2014 at 10:26 PM, Razvan Cojocaru <[email protected]

Quoted 31 lines

wrote:

Quoted 1 line · Aravindh Puthiyaparambil (aravindp)
Quoted 1 line

Xen performs insufficient validation of the contents of mem_event

responses

Quoted 1 line
Quoted 1 line

from the toolstack. As a result, a buggy toolstack could cause Xen to

walk off

Quoted 1 line
Quoted 1 line

the end of a domain's vcpu list, and get out of sync with vcpu pause

reference

Quoted 3 lines
Quoted 3 lines

counts.

These two fixes are compile tested only, as I have no way to plausibly

test the

Quoted 3 lines
Quoted 1 line

mem-event functionality itself.

One easy way of testing is to use the tools/tests/xen-access test

program which exercises mem_access and thereby mem_event. It is fairly easy to run. Bring up a domain and execute " xen-access <domain_id> write|exec". But I understand if you are under time constraints and cannot do it. If you Cc me on these patches, I will gladly test them for you.

Indeed, our application is very xen-access-like (except quite a bit more involved), and I've tested the original patches with 5 different domains 3 times over - but it's a well-behaved citizen of the Xen ecosystem and there were no gimmicks involved. No mem_events piled up, and there was always just one mem_event handler per domain.

Everything went without a hitch, but I did not try to pause the domain while it was running or try to trick the hypervisor in any way.

Quoted 8 lines

Thanks,
Razvan Cojocaru

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#27 lore raw
Quoted 12 lines · Tamas Lengyel

I've also tested the patch with LibVMI and everything works fine. The pause/unpause reference count now does take effect, so the previous issue I reported (a paused domain getting unpaused by mem_event_enable) is fixed by this patch.

One question I have, what if the toolstack wants to unconditionally (force) unpause a domain? Right now with this patch if someone runs 'xl pause domain' a couple times he has no other recourse then to issue 'xl unpause domain' at least the same number of times, or to restart the entire domain. Might be user-friendlier if there was an override provided in case a domain got paused a million times by accident.

Quoted 2 lines

Cheers,
Tamas

I don't think that would be a good idea. The entire point of the proper refcounting is so bits of toolstack subsystems can guarentee that the domain stays paused during a critical set of operations. Providing a "DOMCTL_unpausedomain --force" would undermine the whole purpose of this.

As already expressed, there are plenty of ways a buggy/dumb toolstack can shoot itself in the foot with regards to a domain. I include in this users with dom0 root access and xl.

The two key points are that:

1) a buggy toolstack can't cause Xen perform an unintentional action (e.g. walking off the end of an array, as demonstrated in patch 1 of this series) and
2) several non-buggy parts of a toolstack can operate safely together with respect to a Xen resource.

Any attempt to work around a buggy bit of a toolstack in Xen is effort better spent fixing the toolstack.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel